// Singleton
_p = ww.prototype;

// Application Class
function ww() 
{
	// ==================================================
	// menu
	// ==================================================
	// create a BiJsonLoader
	var loader = new BiJsonLoader();
	// Listen to load event	
	loader.addEventListener("load", this._handledMenu, loader);
	// send login request
	loader.load("menugen.php");
}

// About
_p.aboutus = function()
{
	var win = new BiWindow("Help");
	this.addWindow(win, "www.bedesk.net/marketing/introduction.php", 525);
}

_p._handledMenu = function(e)
{
	// new menu data model with json object
	var oData = e.getTarget().getData();
	var owwMenuDataModel = new wwMenuDataModel(oData);
	var oMenu = application.getComponentById("mbWindows");
	
	// My menu
	owwMenuDataModel.buildModel(oMenu);
	
	// Deberia especificar la opcion donde generar
	
	// Social menu
	var owwSocialMenu = new wwSocialMenu();
	oMenu.add(owwSocialMenu.addSocialMenu());

  // GData Integration: The top rated videos
  var owwYouTube = new wwYouTube();
  oMenu.add(owwYouTube.addYouTube());
	// ==================================================
	// desktop
	// ==================================================
	appWin = application.getWindow();
	
	var ndpTop = oMenu.getHeight() + 1;
	var desktopPane = new BiDesktopPane;
	//backgroundImage="images/bedesk.jpg" backColor="black"
	desktopPane.setLocation(0, ndpTop);
	desktopPane.setRight(0);
	//desktopPane.setBackColor("black");
	//desktopPane.setOpacity(0);

	var taskbar = new BiToolBar;
	taskbar.setLeft(0);
	taskbar.setRight(0);
	taskbar.setBottom(0);
	taskbar.setHeight(28);
	var b = new BiBorder(0);
	b.setTop(2, "outset");
	taskbar.setBorder(b);
	// tooltip
	//taskbar.setToolTip(new BiToolTip("Try right clicking this tool bar"));

	// enter button
	var newWinButton = new BiToolBarButton("Login", new BiImage("images/key.png", 16, 16));
	var btnAbout = new BiToolBarButton("Help", new BiImage("images/help.png", 16, 16));

	var sep = new BiToolBarSeparator;
	
	taskbar.add(newWinButton);
	taskbar.add(sep);
	taskbar.add(btnAbout);
	taskbar.add(new BiToolBarSeparator);

	appWin.add(desktopPane);
	appWin.add(taskbar);

	desktopPane.setBottom(taskbar.getHeight());
	
	// background
	desktopPane.setBackgroundImage("images/background.png");
	//alert(desktopPane.getBackgroundImage());

	// Login
	var oviewLogin = application.getComponentById("viewLogin");
	oviewLogin.addEventListener("beforeclose", function (e) 
	{
		e.preventDefault();
		e.getTarget().setVisible(false);
	}, this);
	newWinButton.addEventListener("action", function (e) 
	{
		oviewLogin.setVisible(true);
		oviewLogin.bringToFront();
		application.getComponentById("vUseEmailLogin").setFocused(true);
	}, this);
	
	// About
	btnAbout.addEventListener("action", function (e) 
	{
		_p.aboutus();
	}, this);
	
	// Register
	var oviewRegister = application.getComponentById("viewRegister");
	oviewLogin.addEventListener("beforeclose", function (e) 
	{
		e.preventDefault();
		e.getTarget().setVisible(false);
	}, this);
	var obLogRegister = application.getComponentById("bLogRegister");
	obLogRegister.addEventListener("action", function (e) 
	{
		oviewRegister.setVisible(true);
		oviewRegister.bringToFront();
		application.getComponentById("vUseName").setFocused(true);
	}, this);
	
	// load combo items
	var ocDelLink = application.getComponentById("cDelLink");
	ocDelLink.removeAll();
	//alert(ocDelLink);
	_p.oviewdbLinks.fillLinks(ocDelLink);
		
	// add to desktop
	_p.desktopPane = desktopPane;
	_p.taskbar = taskbar;

	taskbar.setContextMenu(desktopPane.getWindowManager().getWindowsMenu());
	
	// add window advertise
	var winAds = new BiWindow("Ads by Google");
	_p.addWindow(winAds, "www.bedesk.net/marketing/advertise.php", 550);
	winAds.setHeight(150);
	winAds.setOpacity(.5);
	winAds.setResizable(false);
	winAds.setShowMaximize(false);
	winAds.setShowMinimize(false);
	
	
  /*** Search ***/
	/*var ovSearch = application.getComponentById("viewSearch");
	ovSearch.addEventListener("beforeclose", function (e) 
	{
		e.preventDefault();
		e.getTarget().setVisible(false);
	}, this);
	
	// live search
	btSearch = application.getComponentById("btServSearch");
	var ws = new BiWebService2();
  ws.setAsync(false);
  ws.useService("http://soap.search.msn.com/webservices.asmx?wsdl", "Live Search", false);

	_p.addWindow(ovSearch);*/
	
	/*** Search ***/

  // google gadget clock
  var gGadget = application.getComponentById("beGadget");
  _p.addWindow(gGadget);
}

// ===================================================
// Window Manager
// ===================================================

_p.addWindow = function (win, sURL, width) 
{
	if (!win.getActive())
	{
		//if (Math.random() > 0.66)
		win.setIcon(new BiImage("images/application_xp.png"));
	
		var cp = win.getContentPane();
	
		//alert(win.getWidth());
		/*if (width == undefined)
		{
			if (win.getWidth() == 400)
			{
				win.setSize(900, 400);
			}
		}*/
		/*else
		{
			win.setSize(width, 530);
		}*/

		// opacity		
		win.addEventListener("focus", function (e) 
		{
			win.setOpacity(1);
		});
/*		win.addEventListener("mouseover", function (e) 
		{
			win.setOpacity(1);
		});*/
		win.addEventListener("focusout", function (e) 
		{
			// if the related target is child of the window
			if (!win.contains(e.getRelatedTarget()))
			{
				win.setOpacity(.5);
			}
		});
/*		win.addEventListener("mouseout", function (e) 
		{
			// if the related target is child of the window
			if (!win.contains(e.getRelatedTarget()))
			{
				win.setOpacity(.5);
			}
		});*/

		//win.setSize(640, 480);
		this.desktopPane.add(win);
	
		this.addTaskBarButton(win);
		//win.bringToFront();
		win.setVisible(true);
		//win.setActive(true);
		
		// ==================================================
		// URL
	//	alert(sURL);
		if (sURL != undefined && sURL != null)
		{
			// iframe para mostrar trabajos
			var f = new BiIframe;
			f.setLocation(0,0); f.setRight(0); f.setBottom(0);
			// si no tiene http
			if (sURL.search(/http:/) == '-1') sURL = 'http://' + sURL;
      f.setSrc( sURL );
			cp.add(f);
		}
	}
	
	return win;
};

_p.addTaskBarButton = function (win) {
	// minimized check
	var button = new BiToolBarToggleButton(win.getCaption(), win.getIcon());
	this.taskbar.add(button);
	button.lastState = win.getState();

	win.addEventListener("statechanged", function (e) {
		if (win.getState() != "minimized")
			button.lastState = win.getState();
		else
			button.setChecked(false);
	});

	win.addEventListener("captionchanged", function (e) {
		button.setText(win.getCaption());
	});

	win.addEventListener("activated", function (e) {
		button.setChecked(true);
	});

	win.addEventListener("deactivated", function (e) {
		button.setChecked(false);
	});

	button.addEventListener("action", function (e) {
		if (win.getActive()) {
			win.setState("minimized");
			win.setActive(false);
		}
		else {
			if (win.getState() == "minimized")
				win.setState(button.lastState);
			win.setActive(true);
		}
	});

	// reuse menu from window
	var f = button.getContextMenu();
	button.getContextMenu = function () {
		var m = win.getWindowMenu();
		return m || f.call(this);
	};

	win.addEventListener("close", function (e) {
		this.taskbar.remove(button);
		button.dispose();
	}, this);
};


ww.layoutAndAddComponents = function (oParent, nStart, aItems) {
	var insetTop = oParent.getInsetTop();
	for (var i = 0; i < aItems.length; i++) {
		oParent.add(aItems[i]);
		aItems[i].setLocation(7, nStart - insetTop);
		aItems[i].setRight(7);
		aItems[i].setHeight(14);
		nStart += 14 + 5;
	}
};

// ===================================================
// Menu
// ===================================================
_p.generateMenu = function ()
{
	// create a BiJsonLoader
	var loader = new BiJsonLoader();
	// Listen to load event	
	loader.addEventListener("load", function(e)
	{
		// new menu data model with json object
		var oData = e.getTarget().getData();
		var owwMenuDataModel = new wwMenuDataModel(oData);
		
		var oMenu = application.getComponentById("mbWindows");
		
		// add my menu
		owwMenuDataModel.buildModel(oMenu);
		
		// add social menu
		_p.generateSocialMenu();
		
	}, loader);
	// send login request
	loader.load("menugen.php");	
	
	// reload combo items
	var ocDelLink = application.getComponentById("cDelLink");
	ocDelLink.removeAll();
	//alert(ocDelLink);
	_p.oviewdbLinks.fillLinks(ocDelLink);
}

// social menu
_p.generateSocialMenu = function ()
{
	var oMenu = application.getComponentById("mbWindows");
	var oChildrens = oMenu.getChildren();
	
	// remove social menu
	if (oChildrens[1] != null)	oMenu.remove(oChildrens[1]);
	
	// reload social menu
	var owwSocialMenu = new wwSocialMenu();
	oMenu.add(owwSocialMenu.addSocialMenu());
}

// ===================================================
// Initialize Application
// ===================================================
ww.main = function ()
{ 
	var oWw = new ww;
};

// Users
_p.oviewdbUsers = new viewdbUsers();

// Links
_p.oviewdbLinks = new viewdbLinks();
